#include "mybutton.h"

#include "mylabel.h"

#include <QApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QDebug>
MyButton::MyButton(QWidget *parent) :
    QPushButton(parent),
    m_bLeftPress(false)
{
    this->setWindowFlags(Qt::FramelessWindowHint);    //取消窗体的标题栏
    setMouseTracking(true);
    //MouseMoveEvent为了不太耗资源在默认状态下是要鼠标按下才能捕捉到，要想鼠标不按下时的移动也能捕捉到设置为true
    this->resize(200,50);
    this->setText("按钮");
    //this->setFont(GameManager::Instance().font);
    this->setParent(parent);
    connect(&EventCenter::Instance(),&EventCenter::SelectUIChanged,this,[=](){
        if(!selected)
            return;
        selected = false;
        repaint();
    });
    connect(this,&QPushButton::clicked,this,[=](){
        ClickEvent();
    });
    connect(&EventCenter::Instance(),&EventCenter::UIChange,this,[=](QString name){
        if(info && name == info->name && UILib::Instance().btns.contains(name))
        {
            this->Refresh(UILib::Instance().btns[name]);
        }
    });
}

MyButton::~MyButton()
{
}

bool MyButton::IsSelected()
{
    return selected;
}

void MyButton::mouseMoveEvent(QMouseEvent *event)
{
    if(EventCenter::Instance().GetApplicationMode())
    {
        QPushButton::mouseMoveEvent(event);
        return;
    }
    if(selectDisabled)
        return;
    //窗体不是最大的话就改变鼠标的形状
    if(Qt::WindowMaximized != windowState())
    {
        setCursorShape(CalCursorPos(event->pos(),CalCursorCol(event->pos())));
    }
    //获取当前的点，这个点是全局的
    QPoint ptCurrentPos = QCursor::pos();
    //计算出移动的位置，当前点 - 鼠标左键按下的点
    QPoint ptMoveSize = ptCurrentPos - m_ptViewMousePos;
    QRect rtTempGeometry = m_rtPreGeometry;
    if(m_bLeftPress)
    {
        switch(m_iCalCursorPos)
        {
        case TOPLEFT:
            rtTempGeometry.setTopLeft(m_rtPreGeometry.topLeft()+ptMoveSize);
            break;
        case TOP:
            rtTempGeometry.setTop(m_rtPreGeometry.top()+ptMoveSize.y());
            break;
        case TOPRIGHT:
            rtTempGeometry.setTopRight(m_rtPreGeometry.topRight()+ptMoveSize);
            break;
        case LEFT:
            rtTempGeometry.setLeft(m_rtPreGeometry.left()+ptMoveSize.x());
            break;
        case RIGHT:
            rtTempGeometry.setRight(m_rtPreGeometry.right()+ptMoveSize.x());
            break;
        case BUTTOMLEFT:
            rtTempGeometry.setBottomLeft(m_rtPreGeometry.bottomLeft()+ptMoveSize);
            break;
        case BUTTOM:
            rtTempGeometry.setBottom(m_rtPreGeometry.bottom()+ptMoveSize.y());
            break;
        case BUTTOMRIGHT:
            rtTempGeometry.setBottomRight(m_rtPreGeometry.bottomRight()+ptMoveSize);
            break;
        default:
            break;
        }
        //移动窗体，如果比最小窗体大，就移动
        if(rtTempGeometry.width() >= 5 && rtTempGeometry.height() >= 5)
            setGeometry(rtTempGeometry);

    }
}

void MyButton::mousePressEvent(QMouseEvent *event)
{
    if(EventCenter::Instance().GetApplicationMode())
    {
        QPushButton::mousePressEvent(event);
        return;
    }
    m_iCalCursorPos = CalCursorPos(event->pos(),CalCursorCol(event->pos()));
    if (event->button() == Qt::LeftButton)
    {
        if(m_iCalCursorPos != CENTER)
        {
            m_bLeftPress = true;
        }
        else
        {
            if(selectDisabled)
            {
                selected = false;
                return;
            }
            if(!selected)
            {
                emit EventCenter::Instance().SelectUIChanged();
            }
            selected = !selected;
            repaint();
        }
    }
    m_rtPreGeometry = geometry();
    m_ptViewMousePos = event->globalPos();
}

void MyButton::mouseReleaseEvent(QMouseEvent *event)
{
    if(EventCenter::Instance().GetApplicationMode())
    {
        QPushButton::mouseReleaseEvent(event);
        return;
    }
    m_bLeftPress = false;
    QApplication::restoreOverrideCursor();
}

int MyButton::CalCursorCol(QPoint pt)
{
    return (pt.x() < FRAMESHAPE ? 1 : ((pt.x() > this->width() - FRAMESHAPE) ? 3 : 2));
}

int MyButton::CalCursorPos(QPoint pt, int colPos)
{
    return ((pt.y() < FRAMESHAPE ? 10 : ((pt.y() > this->height() - FRAMESHAPE) ? 30 : 20)) + colPos);
}

void MyButton::setCursorShape(int CalPos)
{
    Qt::CursorShape cursor;
    switch(CalPos)
    {
    case TOPLEFT:
    case BUTTOMRIGHT:
        cursor = Qt::SizeFDiagCursor;
        break;
    case TOPRIGHT:
    case BUTTOMLEFT:
        cursor = Qt::SizeBDiagCursor;
        break;
    case TOP:
    case BUTTOM:
        cursor = Qt::SizeVerCursor;
        break;
    case LEFT:
    case RIGHT:
        cursor = Qt::SizeHorCursor;
        break;
    default:
        cursor = Qt::ArrowCursor;
        break;
    }
    setCursor(cursor);
}

void MyButton::SetSelectedDisabled(bool disable)
{
    selectDisabled = disable;
}

void MyButton::Refresh(ButtonInfo *buttonInfo)
{
    if(!buttonInfo && !info)
        return;
    if(!buttonInfo)
    {
        buttonInfo = info;
    }
    else
    {
        info = buttonInfo;
    }
    buttonInfo->img.replace("\\","/");
    buttonInfo->inImg.replace("\\","/");
    buttonInfo->pressImg.replace("\\","/");
    buttonInfo->soundPath.replace("\\","/");
    QString colorQss = "color:rgb(" + QString::number(buttonInfo->color.red()) + "," +
            QString::number(buttonInfo->color.green()) + "," +
            QString::number(buttonInfo->color.blue()) + ");";

    QString qss = "QPushButton{border-image:url(\"" + buttonInfo->img + "\");" + colorQss + "}";
    if(buttonInfo->img.isEmpty())
        qss = "QPushButton{border:none;" + colorQss + "}";
    if(!buttonInfo->inImg.isEmpty())
        qss +="QPushButton:hover{border-image:url(\"" + buttonInfo->inImg + "\");" + colorQss + "}";
    else
        qss +="QPushButton:hover{border:none;" + colorQss + "}";
    if(!buttonInfo->pressImg.isEmpty())
        qss +="QPushButton:pressed{border-image:url(\"" + buttonInfo->pressImg + "\");" + colorQss + "}";
    else
        qss +="QPushButton:pressed{border:none;" + colorQss + "}";

    this->setStyleSheet(qss);

    this->setFont(buttonInfo->font);
    if(buttonInfo->name != "选项")
        this->setText(buttonInfo->text);
    this->setEnabled(buttonInfo->interactive);
}
void MyButton::paintEvent(QPaintEvent *e)
{
    QPushButton::paintEvent(e);
    if(!selected)
    {
        return;
    }
    QPainter painter(this);
    QPen pen;
    pen.setColor(Qt::red);
    pen.setWidth(3);
    painter.setPen(pen);
    painter.drawRect(this->rect());
    painter.end();
}

void MyButton::ClickEvent()
{
    if(!info)
        return;
    if(!info->soundPath.isEmpty())
    {
        SoundManager::Instance().PlayAppSound(info->soundPath);
    }
    switch(info->event)
    {
    case OPEN:
        qDebug()<<__FILE__<<__LINE__<<"跳转到页面"<<info->eventWidgetName;
        emit EventCenter::Instance().GoToWidget(info->eventWidgetName);
        break;
    case QUIT:
        qDebug()<<"退出游戏";
        QApplication::quit();
        break;
    default:
        break;
    }
}
